package com.gec.hawsteproject.hawaste.controller.advice;

import com.gec.hawsteproject.hawaste.entity.ResponseStatus;
import com.gec.hawsteproject.hawaste.entity.ResultBean;
import com.gec.hawsteproject.hawaste.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.BindException;

/*
* 统一异常处理
* @RestControllerAdvice controller的切面处理类，统一以json返回
* */
@RestControllerAdvice
@Slf4j
public class CommonExceptionHandler {
    /**
     *框架校验数据异常
     * @param e
     * @return
     */
        @ExceptionHandler(BindException.class)
    public ResultBean bindExceptionHandler(BindException e){
            log.error(e.getMessage(),e);
            return ResultBean.fail(ResponseStatus.PARAM_ERROR);
        }
        /*
        * 业务处理异常*/
    @ExceptionHandler(BusinessException.class)
    public ResultBean businessExceptionHandler(BusinessException e){
        log.error(e.getMessage(),e);
        return ResultBean.fail(e.getResponseStatus());
    }

    /*
    * 系统异常
    * */
    @ExceptionHandler(Exception.class)
    public ResultBean exceptionHandler(Exception  e){
        log.error(e.getMessage(),e);
        return ResultBean.error();
    }
}
